package com.xintai.approval.service.impl;

import com.xintai.approval.domain.Condition;
import com.xintai.approval.domain.FlowBasicinfo;
import com.xintai.approval.domain.NodeConfig;
import com.xintai.approval.domain.NodeUser;
import com.xintai.approval.mapper.ConditionMapper;
import com.xintai.approval.mapper.FlowBasicinfoMapper;
import com.xintai.approval.mapper.NodeConfigMapper;
import com.xintai.approval.service.NodeConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class NodeConfigServiceImpl implements NodeConfigService {

    @Resource
    private NodeConfigMapper nodeConfigMapper;

    @Resource
    private ConditionMapper conditionMapper;

    @Resource
    private FlowBasicinfoMapper flowBasicinfoMapper;
    /**
     * 获取开始节点
     * @param flowId
     * @return
     */
    @Override
    public NodeConfig getStart(Long flowId) {
        return nodeConfigMapper.getStart(flowId);
    }

    /**
     * 获取一条节点
     * @param flowId
     * @return
     */
    @Override
    public NodeConfig selectNodeConfig(Long flowId) {
        return nodeConfigMapper.selectNodeConfig(flowId);
    }

    /**
     * 增加流程
     *
     * @param nodeConfig
     */
    @Override
    public void addNodeConfig(NodeConfig nodeConfig) {

        nodeConfigMapper.addNodeConfig(nodeConfig);
    }

    /**
     * 修改下一级节点
     * @param id
     * @param childNodeId
     */
    @Override
    public void upDataNodeConfig(Long id, Long childNodeId) {
        nodeConfigMapper.upDataNodeConfig(id, childNodeId);
    }

    /**
     * 修改多个节点条件
     * @param id
     * @param conditionNodesId
     */
    @Override
    public void upDataNodeConfigCondition(Long id, String conditionNodesId) {
        nodeConfigMapper.upDataNodeConfigCondition(id, conditionNodesId);
    }

    /**
     * 修改多个用户
     * @param id
     * @param nodeUserListIds
     */
    @Override
    public void upDataNodeUserId(Long id, String nodeUserListIds) {
        nodeConfigMapper.upDataNodeUserId(id, nodeUserListIds);
    }

    /**
     * 修改多条条件
     * @param id
     * @param conditionListIds
     */
    @Override
    public void upDataConditionListIds(Long id, String conditionListIds) {
        nodeConfigMapper.upDataConditionListIds(id, conditionListIds);
    }

    /**
     * 获取选择条件
     * @param conditionNodesId
     * @return
     */
    @Override
    public List<NodeConfig> SelectConditionNodes(Long conditionNodesId) {
        return nodeConfigMapper.SelectConditionNodes(conditionNodesId);
    }

    /**
     * 获取节点用户
     * @param nodeUserListId
     * @return
     */
    @Override
    public List<NodeUser> SelectNodeUserList(Long nodeUserListId) {
        return nodeConfigMapper.SelectNodeUserList(nodeUserListId);
    }

    /**
     * 添加流程
     * @param flowBasicinfo
     * @return
     */
    @Override
    public int insertNodeConfig(FlowBasicinfo flowBasicinfo) {
        //返回的发起人
        String flowPermissionids = "";
        List<NodeUser> flowPermission = flowBasicinfo.getFlowPermission();
        for (NodeUser nodeUser : flowPermission) {
            flowPermissionids = flowPermissionids + nodeUser.getTargetId() + ",";
        }
        if (!flowPermissionids.equals("")){
            flowPermissionids = flowPermissionids.substring(0, flowPermissionids.length() - 1);
            //修改定义流程的基本信息（发起人）
            flowBasicinfo.setFlowPermissionid(flowPermissionids);
        }
        NodeConfig nodeConfig = flowBasicinfo.getNodeConfig();
        //流程
        nodeConfigMapper.addNodeConfig(nodeConfig);
        //修改流程基本信息
        flowBasicinfo.setFlowId(nodeConfig.getFlowId());
        flowBasicinfoMapper.updateFlowBasicinfo(flowBasicinfo);
        addNodeConfig(nodeConfig,nodeConfig.getFlowId());
        return 1;
    }

    /**
     * 查询流程
     * @return
     */
    public FlowBasicinfo selectNodeConfig(FlowBasicinfo flowBasicinfo){
        //获取发起人流程
        NodeConfig start = nodeConfigMapper.getStart(flowBasicinfo.getFlowId());
        //获取流程
         NodeConfig nodeConfig = SelectNodeConfig(start);
        //发起人信息
        List<NodeUser> nodeUser = flowBasicinfoMapper.getNodeUser(flowBasicinfo.getId());
        for (NodeUser user : nodeUser) {
            user.setType(1);
        }
        flowBasicinfo.setFlowPermission(nodeUser);
        flowBasicinfo.setNodeConfig(nodeConfig);
        return flowBasicinfo;
    }
    /**
     * 读取流程
     * @param nodeConfig
     * @return
     */
    public NodeConfig SelectNodeConfig(NodeConfig nodeConfig) {
        //下一级
        if (nodeConfig.getChildNodeId()!=null){
            NodeConfig child = nodeConfigMapper.selectNodeConfig(nodeConfig.getChildNodeId());
            SelectNodeConfig(child);
            nodeConfig.setChildNode(child);
        }

        //条件节点
        if (nodeConfig.getConditionNodesId()!=null){
            List<NodeConfig> conditionNodeNodeConfig = nodeConfigMapper.SelectConditionNodes(nodeConfig.getFlowId());
            nodeConfig.setConditionNodes(conditionNodeNodeConfig);
            for (NodeConfig config : conditionNodeNodeConfig) {
                SelectNodeConfig(config);
            }
        }

        //条件
        if (nodeConfig.getConditionListId()!=null){
            List<Condition> conditions = conditionMapper.SelectConditionList(nodeConfig.getFlowId());
            nodeConfig.setConditionList(conditions);
        }else {
            List<Condition> condition = new ArrayList<>();
            nodeConfig.setConditionList(condition);
        }

        //用户
        if (nodeConfig.getNodeUserListId()!=null){
            List<NodeUser> nodeUsers = nodeConfigMapper.SelectNodeUserList(nodeConfig.getFlowId());
            for (NodeUser nodeUser : nodeUsers) {
                nodeUser.setType(1);
            }
            nodeConfig.setNodeUserList(nodeUsers);
        }else {
            List<NodeUser> nodeUser = new ArrayList<>();
            nodeConfig.setNodeUserList(nodeUser);
        }

        return nodeConfig;
    }

    /**
     * 保存
     * @param nodeConfig 参数
     * @param id  上级id
     */
    public void addNodeConfig(NodeConfig nodeConfig,Long id){
        String conditionNodesIds = "";
        String NodeUserListIds = "";
        String conditionListIds ="";
        //条件节点
        if (nodeConfig.getConditionNodes()!=null){
            List<NodeConfig> conditionNodes = nodeConfig.getConditionNodes();
            //遍历
            for (NodeConfig conditionNode : conditionNodes) {
                nodeConfigMapper.addNodeConfig(conditionNode);
                Long flowId = conditionNode.getFlowId();
                conditionNodesIds = conditionNodesIds + flowId + ",";
                addNodeConfig(conditionNode,flowId);
            }
            NodeConfig mynodeConfig = new NodeConfig();
            mynodeConfig.setFlowId(id);

            if ( !conditionNodesIds.equals("") ) {
                conditionNodesIds = conditionNodesIds.substring(0, conditionNodesIds.length() - 1);
                mynodeConfig.setConditionNodesId(conditionNodesIds);
                nodeConfigMapper.upDataNodeConfigCondition(id, conditionNodesIds);
            }
        }

        //用户
        if (nodeConfig.getNodeUserList()!=null){
            List<NodeUser> nodeUserList = nodeConfig.getNodeUserList();
            for (NodeUser nodeUser : nodeUserList) {
                Integer nodeUserId = nodeUser.getTargetId();
                NodeUserListIds= NodeUserListIds + nodeUserId + ",";
            }
            if (!NodeUserListIds.equals("")){
                NodeUserListIds = NodeUserListIds.substring(0, NodeUserListIds.length() - 1);
                nodeConfig.setNodeUserListId(NodeUserListIds);
                nodeConfigMapper.upDataNodeUserId(id,NodeUserListIds);
            }
        }

        //下一级
        if (nodeConfig.getChildNode()!=null){
            NodeConfig childNode = nodeConfig.getChildNode();
            nodeConfigMapper.addNodeConfig(childNode);
            Long id2 = childNode.getFlowId();
            nodeConfigMapper.upDataNodeConfig(id,id2);
            addNodeConfig(childNode,id2);
        }
        //条件数据
        if (nodeConfig.getConditionList()!=null){
            List<Condition> conditionList = nodeConfig.getConditionList();
            for (Condition condition : conditionList) {
                conditionMapper.addCondition(condition);
                Integer columnId = condition.getDataId();
                conditionListIds = conditionListIds + columnId + ",";

            }
            if (!conditionListIds.equals("")){
                conditionListIds = conditionListIds.substring(0,conditionListIds.length()-1);
                nodeConfig.setNodeUserListId(conditionListIds);
                nodeConfigMapper.upDataConditionListIds(id,conditionListIds);
            }
        }
        return;

    }
}
